package com.bjtang.book.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjtang.book.dto.ArticleDto;
import com.bjtang.book.dto.ReturnDto;
import com.bjtang.book.entiy.ArticleBody;
import com.bjtang.book.entiy.ArticleHead;
import com.bjtang.book.entiy.BrowseTable;
import com.bjtang.book.myexception.DataCheckException;
import com.bjtang.book.service.ArticleBodyService;
import com.bjtang.book.service.ArticleHeadService;
import com.bjtang.book.service.BookService;
import com.bjtang.book.service.BrowseService;
import com.bjtang.book.service.SysUserDtoService;
import com.bjtang.book.util.ReturnUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "开放端口")
@Validated
@RestController
@RequestMapping(value = "/open/")
@Transactional(rollbackFor = Exception.class)
public class openController {
	@Autowired
	ArticleHeadService articleHeadService;
	@Autowired
	ArticleBodyService articleBodyService;
	@Autowired
	SysUserDtoService sysUserDtoService;
	@Autowired
	BrowseService browseService;
	@Autowired
	BookService bookService;

	@ApiOperation("查询单个图书商品")
	@GetMapping("/by-book/{bookId}")
	public ReturnDto GetBookById(@PathVariable Long bookId) {
		return ReturnUtil.success(bookService.getById(bookId));
	}

	@SuppressWarnings("unchecked")
	@ApiOperation("查询文章头列表ByTop 最火的前5个")
	@GetMapping("/by-top/")
	public ReturnDto GetArticleByTop() {
		return ReturnUtil.success(articleHeadService.lambdaQuery().eq(ArticleHead::getStatus, 0)
				.orderByDesc(ArticleHead::getTraffic, ArticleHead::getUpNumber, ArticleHead::getRecommendNumber)
				.last("limit 5").list());
	}

	@ApiOperation("查询文章头列表ByNew 最新的")
	@GetMapping("/by-new/{current}/{size}")
	public ReturnDto GetArticleByNew(@PathVariable Long current, @PathVariable Long size) {
		Page<ArticleHead> page = new Page<ArticleHead>(current, size);
		return ReturnUtil.success(articleHeadService.lambdaQuery().eq(ArticleHead::getStatus, 0)
				.orderByDesc(ArticleHead::getArticleNumber).page(page));
	}

	@ApiOperation("查询文章头列表ByFindString")
	@GetMapping("/by-find/{findString}/{current}/{size}")
	public ReturnDto GetArticleByFingStringPage(@PathVariable String findString, @PathVariable Long current,
			@PathVariable Long size) {
		Page<ArticleHead> page = new Page<ArticleHead>(current, size);
		return ReturnUtil.success(articleHeadService.lambdaQuery().eq(ArticleHead::getStatus, 0)
				.like(ArticleHead::getTitle, findString).or().like(ArticleHead::getSummary, findString)
				.orderByDesc(ArticleHead::getArticleNumber).page(page));
	}

	@ApiOperation("查询指定文章ById")
	@GetMapping("/by-article/{articleId}")
	public ReturnDto GetArticleById(@PathVariable Long articleId) {
		ArticleHead findArticle = checkArticle(articleId, "find");
		ArticleBody byId = articleBodyService.getById(articleId);
		String selectUserNameById = sysUserDtoService.selectUserNameById(findArticle.getFounder());
		ArticleDto articleDto = new ArticleDto();
		articleDto.setHead(findArticle);
		articleDto.setBody(byId);
		articleDto.setFounderName(selectUserNameById);
		return ReturnUtil.success(articleDto);
	}

	// 权限校验
	ArticleHead checkArticle(Long articleId, String size) {
		ArticleHead byId = articleHeadService.getById(articleId);
		if (byId == null)
			throw new DataCheckException("文章没找到");
		System.err.println(byId.toString());
		if (byId.getStatus() == 1)
			throw new DataCheckException("文章已失效");
		// 查询
		if ("find".equals(size)) {
			BrowseTable browseTable = new BrowseTable();
			browseTable.setArticleNumber(byId.getArticleNumber());
			browseTable.setFounder(0L);
			browseTable.setUpDownIs(2);
			browseService.save(browseTable);

			byId.setTraffic(byId.getTraffic() + 1);
			articleHeadService.saveOrUpdate(byId);
		}

		return byId;
	}
}
